<template>
  <div id="topic">
    <masker style="border-radius: 2px;">
        <div class="m-img" :style="{backgroundImage: 'url('+topic.img+')'}"></div>
    </masker>
    <h3 class="title">
        {{topic.title}}
    </h3>
    <div style="">
        <img style="width:30px; height:30px; border-radius:50%; overflow:hidden;text-align:center" align="absmiddle" v-if="topic.authImg != null && topic.authImg != ''" src="topic.authImg" />
        <span style="margin-left:10px;color:#CCCCCC;">{{topic.name}}</span>
        <span style="float:right;margin-right:5px">{{topic.createTime}}</span>
    </div>
    <div style="padding: 15px;">
        <h4 style="letter-spacing:3px;display:inline">观点</h4>
        <x-button type="warn" style="width:120px;height:25px;font-size:8px;display:inline;float:right" @click.native="issueOption">发表我的看法</x-button>
        <div style="clear:both"></div>
    </div>
    <div style="height:1px;width:100%;background-color:#e3e3e3;margin-top:10px"></div>
    <div class="comment">
        <scroller style="top: 300px;"
              ref="commentScroller"
              :on-refresh="refreshComment"
              :on-infinite="infiniteComment">
        
        <div>
        <ul>
            <li v-for="item in comments"  style="border-bottom: dotted 1px rgb(16, 197, 182);">
                <div style="padding: 15px;">
                    <img style="width:30px; height:30px; border-radius:50%; overflow:hidden;text-align:center" align="absmiddle" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1498366120004&di=c5003db59af6fd4484d7333cfaf84e90&imgtype=0&src=http%3A%2F%2Fimg15.3lian.com%2F2015%2Fh1%2F03%2Fd%2F111.jpg" />
                    <span style="margin-left:2px;color:#CCCCCC;font-size:12px">{{item.commentByName}}</span>
                    <p style="margin-left:27px;padding:5px;font-size:14px">{{item.commentContent}}</p>
                    <span style="color:#CCCCCC;margin-top:5px;font-size:14px;float:right">{{item.commentTimes}}</span>
                    <div style="clear:both"></div>
                </div>
            </li>
        </ul>
        </div>
        </scroller>
    </div>
    <div class="issue_comment" v-if="commentShow">
      <group>
          <x-textarea :max="50" placeholder="我的意见" v-model="content" ref='content'></x-textarea>
      </group>
      
      <x-button :text="submit001" :disabled="disable001" class="comment_btn" @click.native="processButton001" type="primary"></x-button>

      <toast v-model="showPositionValue" type="text" :time="1000" is-show-mask text="请退出重新发布" :position="position"></toast>
    </div>
    </div>
</template>

<script>
import { XButton, XTextarea, Group, Toast, Masker } from 'vux'
export default {
  components: {
    XButton,
    XTextarea,
    Group,
    Toast,
    Masker
  },
  data () {
    return {
      topicId: 0,
      noData: '',
      currentPage: '0',
      comments: [],
      items: [],
      submit001: '发布',
      disable001: false,
      content: '',
      position: 'default',
      showPositionValue: false,
      commentShow: false,
      topic: {
        title: null,
        content: null,
        discription: null,
        img: null,
        name: null,
        authImg: null
      }
    }
  },
  mounted () {
    console.log('mounted')
    console.log(this.$router.currentRoute.query.id)
    this.topicId = this.$router.currentRoute.query.id
    if (this.$router.history.current.path === '/specialColumn/columnContentList') {
      console.log('路径是一个specialColumn/columnContentList')
    }
    let config = {
      headers: {'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/json', 'Accept': 'application/json'},
      data: {
        'id': this.topicId
      },
      responseType: 'json',
      method: 'post',
      currentPage: '0'
    }
    var self = this
    this.$ajax('api/cms/home/topics/info', config).then(function (response) {
      console.log(response)
      console.log(response.data.code)
      console.log(response.data.data)
      self.topic = response.data.data
      console.log(self.topic.authImg)
    }).catch(function (error) {
      console.log(error)
    })
  },
  methods: {
    refreshComment: function (done) {
      console.log('下拉刷新评论数据')
      console.log('refreshComment')
      var self = this
      this.noData = true
      setTimeout(() => {
        self.points = []
        let config = {
          headers: {'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/json', 'Accept': 'application/json'},
          data: {
            'orgNo': '1',
            'currentPage': '0',
            'pageSize': '10',
            'createBy': '1'
          },
          responseType: 'json',
          method: 'post'
        }
        self.$ajax('api/cms/home/topics/queryCommit', config).then(function (response) {
          console.log(response)
          console.log(response.data.data.length)
          console.log(response.data.pagination.currentPage)
          self.currentPage = response.data.pagination.currentPage + 1
          self.comments = response.data.data
        }).catch(function (error) {
          console.log(error)
        })
        done()
      }, 1500)
    },
    infiniteComment: function (done) {
      console.log('上拉加载评论数据')
      console.log('infiniteComment')
      var self = this
      if (this.noData) {
        console.log(this.noData)
        this.noData = false
        self.$refs.commentScroller.finishInfinite(true)
        return
      }
      setTimeout(() => {
        let config = {
          headers: {'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/json', 'Accept': 'application/json'},
          data: {
            'orgNo': '1',
            'currentPage': self.currentPage,
            'pageSize': '10',
            'id': self.topicId
          },
          responseType: 'json',
          method: 'post'
        }
        self.$ajax('api/cms/home/topics/queryCommit', config).then(function (response) {
          console.log(response)
          console.log(response.data.code)
          var code = response.data.code
          self.currentPage = response.data.pagination.currentPage + 1
          if (code === '01') {
            self.noData = '没有更多数据'
            self.$refs.commentScroller.finishInfinite(true)
            return
          }
          var data = response.data.data
          var start = data.length
          for (var i = 0; i < start; i++) {
            self.comments.push(data[i])
          }
          self.bottom = self.bottom + 10
          done()
        }).catch(function (error) {
          console.log(error)
        })
      }, 1500)
    },
    showPosition: function (position) {
      this.position = position
      this.showPositionValue = true
    },
    processButton001 () {
      this.submit001 = 'processing'
      this.disable001 = true
      var txt = this.content
      var self = this
      let config = {
        headers: {'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/json', 'Accept': 'application/json'},
        data: {
          'commentBy': '1',
          'content': txt,
          'targetId': self.topicId
        },
        responseType: 'json',
        method: 'post',
        currentPage: '0'
      }
      this.$ajax('api/cms/home/topics/addComment', config).then(function (response) {
        console.log(response)
        console.log(response.data.data)
        console.log(response.data.data.comment)
        if (response.data.code === '00') {
          self.commentShow = false
          self.comments.unshift(response.data.data.comment)
          this.submit001 = '发布'
          this.disable001 = false
        } else {
          self.showPosition('top')
        }
      }).catch(function (error) {
        console.log(error)
      })
    },
    issueOption () {
      this.commentShow = true
    }
  }
}
</script>

<style scoped>
    .m-img {
        width:100%;
        height:150px;
    }
    .title {
        font-size:24px;
        padding:5px;
        letter-spacing:3px;
        text-align:center;
    }
    li {
        list-style-type:none;
    }
    .comment {
        margin-top:10px;
    }
    .issue_comment {
      position: absolute;
      width:97.5%;
      bottom: 2px;
      background:#e3e3e3;
      padding: 5px;
    }
    .comment_btn {
      background-color:#eeeeee !important;
      height:30px;
      width:80px;
      color:#000;
      font-size:8px;
      letter-spacing:5px;
      margin-top:5px;
      float:right;
    }
</style>
